define(['jquery', 'bootstrap', 'backend', 'table', 'form'],function($, undefined, Backend, Table, Form){
    var Controller = {
        index:function(){
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'subject/subject/index',
                    add_url: 'subject/subject/add',
                    edit_url: 'subject/subject/edit',
                    del_url: 'subject/subject/del',
                    multi_url: 'subject/subject/multi',
                    table: 'subject',
                }
            });

            // 获取到table标签的id
            var table = $("#table")

            // 初始化表格
            table.bootstrapTable({
                // 当访问列表发起ajax请求数据
                url:$.fn.bootstrapTable.defaults.extend.index_url,
                // 主键
                pk:'id',
                // 设置默认排序
                sortName:'createtime',
                sortOrder:'desc',
                columns:[
                    [
                        {checkbox: true},
                        {field:'id',title:__('Id'),sortable: true},
                        {field:'title',title:__('Title')},
                        {field:'thumb_cdn',title:__('Thumbs'),operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field:'price',title:__('Price'),sortable: true},
                        {field:'like_count',title:__('Likes')},
                        {field:'category.name',title:__('CateName')},
                        {field:'createtime',title:__('Ceatetime'),addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons:[
                                {
                                    name:'chapter',
                                    title:'课程章节',
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    icon:'fa fa-list',
                                    url:'subject/chapter/index'
                                }
                            ]
                        }
                    ]
                ]
            })

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        // 添加
        add:function()
        {
            Controller.api.bindevent();
        },
        edit:function()
        {
            Controller.api.bindevent();
        },
        recyclebin:function()
        {
            // 初始化表格参数配置
            Table.api.init({
                extend:{
                    recyclebin_url:'subject/subject/recyclebin',
                    restore_url:'subject/subject/restore',
                    del_url:'subject/subject/destroy',
                    table: 'subject',
                }
            })

            // 获取到table标签的id
            var table = $("#table")

            // 初始化表格
            table.bootstrapTable({
                // 当访问列表发起ajax请求数据
                url:$.fn.bootstrapTable.defaults.extend.recyclebin_url,
                // 主键
                pk:'id',
                // 设置默认排序
                sortName:'createtime',
                columns:[
                    [
                        {checkbox: true},
                        {field:'id',title:__('Id'),sortable: true},
                        {field:'title',title:__('Title')},
                        {field:'thumb_cdn',title:__('Thumbs'),operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field:'price',title:__('Price'),sortable: true},
                        {field:'like_count',title:__('Likes')},
                        {field:'category.name',title:__('CateName')},
                        {field:'createtime',title:__('Ceatetime'),addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field:'deletetime',title:__('Deletetime'),addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons:[
                                {
                                    name:'restore',
                                    title:'还原数据',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon:'fa fa-reply',
                                    url:$.fn.bootstrapTable.defaults.extend.restore_url,
                                    confirm:'确认还原该条数据？',
                                    success:function() {
                                        // 操作完成后是否刷新列表
                                        table.bootstrapTable('refresh')
                                    }
                                }
                            ]
                        }
                    ]
                ]
            })

            $(document).on('click','.btn-restore',function(){
                Layer.confirm(__('确认还原选择数据？'),function(index){
                    var ids = Table.api.selectedids(table)

                    // fastAdmin封装ajax请求
                    Fast.api.ajax({
                        url:$.fn.bootstrapTable.defaults.extend.restore_url,
                        data:{
                            ids
                        }
                    },function(){
                        // 关闭确认框
                        Layer.close(index)

                        // 刷新表格
                        table.bootstrapTable('refresh')
                    })
                })
            })

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    }


    return Controller
})